var invalidWord;
(async function(){
    let data = await $.get('ajax/getInvalidword.php');
    invalidWord = JSON.parse(data).data;
    
})()
function tab(obj){
    if(event.keyCode==9){
        obj.value=obj.value+'    ';
        if(event && event.preventDefault){
            event.preventDefault();
        }else{
            window.event.returnValue=false;
        }
    }
}
let G = {};
let fj_num = $('#glfj>b');
    G.send_btn = $('#send-btn');
    G.inputs = $('[ns-data=true]');
    G.inputs.keydown(function(){
        $(this).removeClass('err')
    });
    G.send_btn.click(function(e){
        let err = true;
        let sendcontent = $('#sendcontent');
        let sendtitle = $('#sendtitle');
        if($.trim(sendtitle.val()) === '' || sendtitle.val().length > 30){
            sendtitle.focus();
            sendtitle.addClass('err')
        }else if(fj_num.text() == 0 && $.trim(sendcontent.val()) === ''){
            sendcontent.focus();
            sendcontent.addClass('err')
        }else{
            let flag = invalidWord.some(d=>sendcontent.val().indexOf(d) !== -1 || sendtitle.val().indexOf(d) !== -1);
            if(flag){
                e ? e.preventDefault() : window.e.returnValue = false;
                alert('输入内容不能包含敏感词汇！')
                return
            }
            err = false;
        }
        if(err){
            e ? e.preventDefault() : window.e.returnValue = false;
        }
    });
let glfj = $('#glfj');

let x = $('#x');
let up_list = $('#upload-zone');
glfj.click(function(event) {
     up_list.show()
});
x.click(function(event) {
    up_list.hide()
});
//文件上传
let file = $('#img');
let reader = new FileReader();
file.change(function(event) {
    if(!this.files.length || !/^image\/\w+/.test(this.files[0].type)){
        return
    }else{
        let filename = this.files[0].name;
        reader.readAsDataURL(this.files[0]);
        reader.onload=function(){
            $.ajax({
                url: 'ajax/upload.php',
                type: 'POST',
                cache: false, //不缓存上传文件
                data: {type: 0,files: this.result}
            })
            .done(function(data) {
                fj_num.text(parseInt(fj_num.text())+1);
                up_list.append(`<li><span class="file-name">${filename}</span><span onclick="del(this)" class="file-action hand">删除</span><input type="hidden" name="files[]" value="${data.file}"></li>`);
            });
        }
    }
});
function del(el){
    let delfile = $(el).next().val();
     $.ajax({
        url: 'ajax/upload.php',
        type: 'POST',
        data: {type: 'cancle',file: delfile}
    })
    .done(function() {
        $(el).parent().remove();
        fj_num.text(parseInt(fj_num.text())-1);
    });
}

//表情包
$('#emoji').click(function() {
    $(this).toggleClass('sel-bd');
    $('.bq-zone').toggle();
});
function addsmile(smilecode){
    let body = $('#sendcontent');
    let original_val = body.val();
    body.val(original_val + smilecode);
    body.focus();
}